package com.cagtwitter.services;

import java.util.List;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cagtwitter.hibernate.AboutCelebTweet;
import com.cagtwitter.hibernate.AboutTvShowTweet;
import com.cagtwitter.hibernate.CelebTweet;
import com.cagtwitter.hibernate.Celebrity;
import com.cagtwitter.hibernate.CelebrityDao;
import com.cagtwitter.hibernate.TvShowDao;
import com.cagtwitter.hibernate.TvShow;

public class DatabaseServices {
	public String getCelebritiesAsJSon(String matches_string){
		JSONArray resultArray = new JSONArray();
		JSONArray jsonArray = new JSONArray();
		JSONObject result = new JSONObject();
		CelebrityDao hbs = new CelebrityDao();
		List<Celebrity> celebrityList;
		
		if(matches_string == null)
			celebrityList = hbs.getCelebrities();
		else celebrityList = hbs.getCelebritiesMatches(matches_string);
		
		try {
			for(Celebrity celeb : celebrityList){
				JSONObject json = new JSONObject();
				json.put("screen_name", celeb.getUsername());
				json.put("firstname", celeb.getFirstName());
				json.put("lastname", celeb.getLastName());

				jsonArray.put(json);
			}
		
		result.put("celebs",jsonArray);
		
		resultArray.put(result);
		
		}catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return resultArray.toString();
	}
	
	public String getTvShowsAsJSon(){
		JSONArray resultArray = new JSONArray();
		JSONArray jsonArray = new JSONArray();
		JSONObject result = new JSONObject();
		TvShowDao htvshow = new TvShowDao();
		List<TvShow> tvShowList;
		
		tvShowList = htvshow.getTvShows();
		
		try {
			for(TvShow tvShow : tvShowList){
				JSONObject json = new JSONObject();
				
				/*String[] keywordTokens = tvShow.getKeywords().split(",");
				for (int x=0; x<keywordTokens.length; x++)
			    	jsonKeywordArray.put(keywordTokens[x]);*/
				String keywords = tvShow.getKeywords();
				
				json.put("keywords",keywords);
				json.put("name", tvShow.getName());
				json.put("show_name", tvShow.getShowName());
				jsonArray.put(json);
			}
		
		result.put("tvShows",jsonArray);
		
		resultArray.put(result);
		
		}catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return resultArray.toString();
	}
	
	public String getCelebTweetsAsJSon(String name,int page,int count){
		CelebrityDao hibernateCelebrity = new CelebrityDao();
		List<CelebTweet> celebTweetList = hibernateCelebrity.getCelebrityTweets(name,page,count);

		JSONArray jsonCelebs = new JSONArray();
		JSONArray jsonResult = new JSONArray();
		JSONObject jsonCount = new JSONObject();
		JSONObject jsonCelebsObj = new JSONObject();
		try {
			for(CelebTweet ct : celebTweetList){
				JSONObject json = new JSONObject();
				
				json.put("name",ct.getName());
				json.put("tweetimg",ct.getTweetimg());
				json.put("tweetname",ct.getTweetname());
				json.put("tweetfullname",ct.getTweetfullname());
				json.put("tweetdate",ct.getTweetdate());
				json.put("tweettext",ct.getTweettext());
				json.put("twid",ct.getTwid());
				jsonCelebs.put(json);
			}
			jsonCelebsObj.put("tweets",jsonCelebs);
			jsonCount.put("count",hibernateCelebrity.getCelebrityTweetCount(name,BaseParams.CELEBRITY));
			jsonResult.put(jsonCount);
			jsonResult.put(jsonCelebsObj);
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jsonResult.toString();
	}
	
	public String getAboutCelebTweetsAsJSon(String name,int page,int count){
		CelebrityDao hibernateCelebrity = new CelebrityDao();
		List<AboutCelebTweet> aboutCelebTweetList = hibernateCelebrity.getAboutCelebrityTweets(name,page,count);

		JSONArray jsonCelebs = new JSONArray();
		JSONArray jsonResult = new JSONArray();
		JSONObject jsonCount = new JSONObject();
		JSONObject jsonAboutCelebs = new JSONObject();
		try {
			for(AboutCelebTweet ct : aboutCelebTweetList){
				JSONObject json = new JSONObject();
				
				json.put("name",ct.getName());
				json.put("tweetimg",ct.getTweetimg());
				json.put("tweetname",ct.getTweetname());
				json.put("tweetfullname",ct.getTweetfullname());
				json.put("tweetdate",ct.getTweetdate());
				json.put("tweettext",ct.getTweettext());
				json.put("twid",ct.getTwid());
				jsonCelebs.put(json);
			}
			jsonAboutCelebs.put("tweets",jsonCelebs);
			jsonCount.put("count",hibernateCelebrity.getCelebrityTweetCount(name,BaseParams.ABOUT));
			jsonResult.put(jsonCount);
			jsonResult.put(jsonAboutCelebs);
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jsonResult.toString();
	}
	
	public String getAboutTvShowTweetsAsJSon(String name,int page,int count){
		TvShowDao hibernateTvShow = new TvShowDao();
		List<AboutTvShowTweet> aboutTvShowTweetList = hibernateTvShow.getAboutTvShowTweets(name,page,count);

		JSONArray jsonTvShow = new JSONArray();
		JSONArray jsonResult = new JSONArray();
		JSONObject jsonCount = new JSONObject();
		JSONObject jsonAboutTvShow = new JSONObject();
		try {
			for(AboutTvShowTweet ct : aboutTvShowTweetList){
				JSONObject json = new JSONObject();
				
				json.put("name",ct.getName());
				json.put("tweetimg",ct.getTweetimg());
				json.put("tweetname",ct.getTweetname());
				json.put("tweetfullname",ct.getTweetfullname());
				json.put("tweetdate",ct.getTweetdate());
				json.put("tweettext",ct.getTweettext());
				json.put("twid",ct.getTwid());
				jsonTvShow.put(json);
			}
			jsonAboutTvShow.put("tweets",jsonTvShow);
			jsonCount.put("count",hibernateTvShow.getTvShowTweetCount(name,BaseParams.ABOUT));
			jsonResult.put(jsonCount);
			jsonResult.put(jsonAboutTvShow);
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jsonResult.toString();
	}
	
	public void refreshCelebrityTweetOnDB(){
		CelebrityDao hbc = new CelebrityDao();
		List<Celebrity> celebrities = hbc.getCelebrities();
		WebDataServices wds = new WebDataServices();
		
		for(Celebrity celeb : celebrities){
			Properties properties = new Properties();
			String lastTwid = hbc.getLastTwid(celeb.getUsername(),BaseParams.CELEBRITY);
			if(!lastTwid.equals("0")) properties.setProperty("since_id",lastTwid);
			properties.setProperty("count","100");
			properties.setProperty("include_rts","true");
			
			wds.writeCelebTweetsToDB(celeb.getUsername(),properties);
		}
	}
	
	public void refreshAboutCelebrityTweetOnDB(){
		CelebrityDao hbc = new CelebrityDao();
		List<Celebrity> celebrities = hbc.getCelebrities();
		WebDataServices wds = new WebDataServices();
		
		
		for(Celebrity celeb : celebrities){
			Properties properties = new Properties();
			String lastTwid = hbc.getLastTwid(celeb.getUsername(),BaseParams.ABOUT);
			if(!lastTwid.equals("0")) properties.setProperty("since_id",lastTwid);
			properties.setProperty("include_rts","true");
			properties.setProperty("rpp","100");
			wds.writeAboutCelebTweetsToDB(celeb,properties);
		}
	}
	
	public void refreshAboutTvShowTweetOnDB(){
		TvShowDao hbtv = new TvShowDao();
		List<TvShow> tvShows = hbtv.getTvShows();
		WebDataServices wds = new WebDataServices();
		
		for(TvShow tvshow:tvShows){
			Properties properties = new Properties();
			String lastTwid = hbtv.getLastTwid(tvshow.getName(),BaseParams.ABOUT);
			if(!lastTwid.equals("0")) properties.setProperty("since_id",lastTwid);
			properties.setProperty("include_rts","true");
			properties.setProperty("rpp","50");
			wds.writeAboutTvShowTweetsToDB(tvshow.getShowName(), properties);
		}
	}

}
